package com.service.school.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.TStudent;
import com.model.TTeacher;
import com.pageModel.DataGrid;
import com.pageModel.Teacher;
import com.service.school.TeacherServiceI;

@Service(value = "teacherService")
public class TeacherServiceImpl implements TeacherServiceI {

	private BaseDaoI<TTeacher> teacherDao;

	public BaseDaoI<TTeacher> getTeacherDao() {
		return teacherDao;
	}

	@Autowired
	public void setTeacherDao(BaseDaoI<TTeacher> teacherDao) {
		this.teacherDao = teacherDao;
	}

	@Override
	public DataGrid datagrid(Teacher teacher) {
		DataGrid dg = new DataGrid();
		String hql = "from TTeacher t where 1=1 ";
		if (null != teacher.getTeacherName() && !teacher.getTeacherName().trim().equals("")) {
			hql += "and t.teacherName like '%" + teacher.getTeacherName().trim() + "%'";
		}
		if (null != teacher.getTeacherId() && !teacher.getTeacherId().trim().equals("")) {
			hql += "and t.teacherId like '%" + teacher.getTeacherId().trim() + "%'";
		}
		if (null != teacher.getMajorName() && !teacher.getMajorName().trim().equals("")) {
			hql += "and t.majorName like '%" + teacher.getMajorName().trim() + "%'";
		}
		String totalHql = "select count(*) " + hql;
		List<TTeacher> l = teacherDao.find(hql, teacher.getPage(), teacher.getRows());
		List<Teacher> nl = new ArrayList<Teacher>();
		if (l != null && l.size() > 0) {
			for (TTeacher t : l) {
				Teacher u = new Teacher();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
		dg.setTotal(teacherDao.count(totalHql));
		dg.setRows(nl);
		return dg;
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete TTeacher t where t.id in(";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		teacherDao.executeHql(hql);
	}

	@Override
	public boolean add(Teacher teacher) {
		Long count = teacherDao.count("select count(*) from TTeacher obj where obj.teacherId=?", new Object[] {teacher.getTeacherId()});
		if (count != null && count > 0) {
			 return  false;
		}
		TTeacher t=new TTeacher();
		if((null!=teacher.getTeacherId()||!teacher.getTeacherId().trim().equals(""))&&(null!=teacher.getTeacherName())){
			t.setId(UUID.randomUUID().toString());
			t.setTeacherId(teacher.getTeacherId());
			t.setTeacherName(teacher.getTeacherName());
			t.setSex(teacher.getSex());
			t.setIsSpecial("否");
			if(null!=teacher.getDepartment()||!teacher.getDepartment().trim().equals("")){
				t.setDepartment(teacher.getDepartment());
			}
			if(null!=teacher.getTitle()||!teacher.getTitle().trim().equals("")){
				t.setTitle(teacher.getTitle());
			}
			if(null!=teacher.getOrganization()||!teacher.getOrganization().trim().equals("")){
				t.setOrganization(teacher.getOrganization());
			}
			if(null!=teacher.getMajorName()||!teacher.getMajorName().trim().equals("")){
				t.setMajorName(teacher.getMajorName());
			}
			if(null!=teacher.getBornDate()||!teacher.getBornDate().equals("")){
				t.setBornDate(teacher.getBornDate());
			}
			if(null!=teacher.getIdentityCard()){
				t.setIdentityCard(teacher.getIdentityCard());
			}
			if(null!=teacher.getHomeTel()){
				t.setHomeTel(teacher.getHomeTel());
			}
			if(null!=teacher.getPhone()){
				t.setPhone(teacher.getPhone());
			}
			if(null!=teacher.getAddress()||!teacher.getAddress().trim().equals("")){
				t.setAddress(teacher.getAddress());
			}
		}
		teacherDao.save(t);
		return true;
	}

	@Override
	public void edit(Teacher teacher) {
		TTeacher t=teacherDao.get(TTeacher.class, teacher.getId());
		BeanUtils.copyProperties(teacher, t);
	}
}
